package com.group9.service.impl;

import com.group9.dto.CommentMessageInfo;
import com.group9.dto.CommentMessages;
import com.group9.pojo.CommentMessage;
import com.group9.repository.CommentMessageRepository;
import com.group9.service.CommentMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lcouncillor on 2020/6/9.
 */
@Service
public class CommentMessageServiceImpl implements CommentMessageService {

    @Autowired
    CommentMessageRepository commentMessageRepository;

    @Override
    public List<CommentMessageInfo> showAllMessage(String receiverId) {
        return null;
    }

    @Override
    public List<CommentMessages> showReadedMessage(String receiverId) {
        //通过接收者id搜索到所有已读消息
        List<CommentMessage> commentMessageList = commentMessageRepository.selectReadedMessageByReceiverId(receiverId);
        //通过commentId再分类，输出List<CommentMessages>
        List<CommentMessages> commentMessagesList = classifyByCommentId(commentMessageList);
        return commentMessagesList;
    }

    @Override
    public List<CommentMessages> showNoReadMessage(String receiverId) {
        //通过接收者id搜索到所有未读消息
        List<CommentMessage> commentMessageList = commentMessageRepository.selectNoReadMessageByReceiverId(receiverId);
        //通过commentId再分类，输出List<CommentMessages>
        List<CommentMessages> commentMessagesList = classifyByCommentId(commentMessageList);
        return commentMessagesList;
    }

    @Override
    public Integer addCommentMessage(CommentMessage commentMessage) {
        commentMessageRepository.insertCommentMessage(commentMessage);
        return 1;
    }

    @Override
    public Integer saveCommentMessageStatus(String commentId, String userId) {
        List<CommentMessage> commentMessages = commentMessageRepository.selectByCommentIdAndUserId(commentId, userId);
        return commentMessageRepository.updateStatus(commentMessages);
    }

    @Override
    public Integer deleteMessage(String id) {
        return null;
    }


    /**
     * 通过commentId再分类，输出List<CommentMessages>
     * @param commentMessages
     * @return
     */
    private List<CommentMessages> classifyByCommentId(List<CommentMessage> commentMessages){
        List<CommentMessages> commentMessagesList = new ArrayList<>();

        for (int i=0; i<commentMessages.size(); i++){
            CommentMessageInfo commentMessageInfo = new CommentMessageInfo();

            int index = 0;
            for (int j=0; j<commentMessagesList.size(); j++){
                if (commentMessagesList.get(j).getCommentId().equals(commentMessages.get(i).getCommentId())){
                    List<CommentMessageInfo> commentMessageInfoList = commentMessagesList.get(j).getCommentMessageInfos();

                    commentMessageInfo.setMessageId(commentMessages.get(i).getId());
                    commentMessageInfo.setMessageStatus(1);
                    commentMessageInfo.setReceiverId(commentMessages.get(i).getReceiverId());

                    commentMessageInfoList.add(commentMessageInfo);
                    commentMessagesList.get(j).setCommentMessageInfos(commentMessageInfoList);
                    break;
                }else {
                    index++;
                }
            }

            if (index == commentMessagesList.size()){
                CommentMessages commentMessages1 = new CommentMessages();

                String commentId = commentMessages.get(i).getCommentId();
                commentMessageInfo.setMessageId(commentMessages.get(i).getId());
                commentMessageInfo.setReceiverId(commentMessages.get(i).getReceiverId());
                commentMessageInfo.setMessageStatus(commentMessages.get(i).getStatus());

                commentMessages1.setCommentId(commentId);
                commentMessages1.setCommentSenterName(commentMessages.get(i).getCommentSenterName());

                List<CommentMessageInfo> commentMessageInfoList = new ArrayList<>();
                commentMessageInfoList.add(commentMessageInfo);
                commentMessages1.setCommentMessageInfos(commentMessageInfoList);
                commentMessagesList.add(commentMessages1);
            }

        }
        return commentMessagesList;
    }
}
